using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AntEater.ComponentModel;

namespace AntEater.Runtime.UI
{
    /// <summary>
    /// Type of External Build Properties to display 
    /// </summary>
    internal enum ExternalBuildPropertiesEditListViewType
    {
        /// <summary>
        /// Display External Build Properties defined for the machine.
        /// </summary>
        Machine = 0,

        /// <summary>
        /// Display External Build Properties defined for the solution.
        /// </summary>
        Solution = 1,

        /// <summary>
        /// Display External Build Properties defined for the build file.
        /// </summary>
        BuildFile = 2,
    }

    /// <summary>
    /// View for editing a External Build Properties Collection
    /// </summary>
    internal partial class ExternalBuildPropertiesEditListView : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalBuildPropertiesListView"/> class.
        /// </summary>
        public ExternalBuildPropertiesEditListView() {
            InitializeComponent();
            this._viewType = ExternalBuildPropertiesEditListViewType.Machine;
        }

        private ExternalBuildPropertiesEditListViewType _viewType;

        /// <summary>
        /// Gets or sets the type of the view.
        /// </summary>
        /// <value>The type of the view.</value>
        public ExternalBuildPropertiesEditListViewType ViewType {
            get { return _viewType; }
            set { _viewType = value; }
        }

        /// <summary>
        /// Gets the external build properties.
        /// </summary>
        /// <value>The external build properties.</value>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ExternalBuildPropertyCollection ExternalBuildProperties {
            get {
                ExternalBuildPropertyCollection externalBuildProperties = null;

                switch (_viewType) {
                    case ExternalBuildPropertiesEditListViewType.Solution:
                        externalBuildProperties = propertiesListView.SolutionExternalBuildProperties;
                        break;

                    case ExternalBuildPropertiesEditListViewType.BuildFile:
                        externalBuildProperties = propertiesListView.BuildFileExternalBuildProperties;
                        break;

                    case ExternalBuildPropertiesEditListViewType.Machine:
                    default:
                        externalBuildProperties = propertiesListView.MachineExternalBuildProperties;
                        break;
                }

                return externalBuildProperties;
            }

        }

        /// <summary>
        /// Forces the control to invalidate its client area and immediately redraw itself and any child controls.
        /// </summary>
        /// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
        public override void Refresh() {
            propertiesListView.Refresh();
            removeButton.Enabled = propertiesListView.SelectedExternalBuildProperty != null;
            base.Refresh();
        }

        /// <summary>
        /// Adds the clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void AddClicked(object sender, EventArgs e) {

            // Show build file selector
            ExternalBuildPropertyView view = new ExternalBuildPropertyView();

            // Add choosen build file to the project
            if (view.ShowDialog() == DialogResult.OK) {
                this.ExternalBuildProperties.Add(view.ExternalBuildProperty);
            }

            this.Refresh();
        }

        /// <summary>
        /// Removes the clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void RemoveClicked(object sender, EventArgs e) {
            if (propertiesListView.SelectedExternalBuildProperty != null) {
                this.ExternalBuildProperties.Remove((ExternalBuildProperty)propertiesListView.SelectedExternalBuildProperty);
            }
            this.Refresh();
        }

        /// <summary>
        /// Propertieses the list view selected index changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void PropertiesListViewSelectedIndexChanged(object sender, EventArgs e) {
            removeButton.Enabled = propertiesListView.SelectedExternalBuildProperty != null;
        }

        /// <summary>
        /// Lists the double clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void PropertiesListViewDoubleClicked(object sender, EventArgs e) {

            if (propertiesListView.SelectedExternalBuildProperty != null) {

                // Show build file selector
                ExternalBuildPropertyView view = new ExternalBuildPropertyView();
                view.ExternalBuildProperty = propertiesListView.SelectedExternalBuildProperty;
                view.Refresh();

                // Add choosen build file to the project
                if (view.ShowDialog() == DialogResult.OK) {
                    this.ExternalBuildProperties.Remove(propertiesListView.SelectedExternalBuildProperty);
                    this.ExternalBuildProperties.Add(view.ExternalBuildProperty);
                }

                this.Refresh();
            }


        }


    }
}
